<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/common/taglibs.jsp" %>
<%@ page import="com.starbaba.core.web.constant.WebConstants"%>
<%@ page import="com.starbaba.system.user.model.UserVO"%>
<!DOCTYPE html>
<html lang="zh-cn">
	<head>
	<%@ include file="/common/common-css.jsp" %>
	<%
		String userno=request.getParameter("userno");
		String mobile=request.getParameter("mobile");
		
		request.setAttribute("userno",userno);
		request.setAttribute("mobile",mobile);
	%>
		
	</head>
<body  class="no-skin">
			<div class="page-content" >
					<div class="page-content-area">
							<div class="page-header">
								<h1>
									修改密码
								</h1>
							</div>
				
							<div class="row">
							
								 <div class="col-sm-12">
							 		<div class="widget-box">
										<div class="widget-header widget-header-blue widget-header-flat">
										<h1 class="widget-title lighter">
										
										</h1>
									</div>
		
									<div class="widget-body" style="height:800px;overflow:auto">
										<div class="widget-main">
								 		<form class="form-horizontal" id="validation-form">
								 		
								 		<input type="hidden" name="userid" id="userid" value="<%=((UserVO) request.getSession().getAttribute(WebConstants.SESSION_ATTRIBUTE_USER)).getUserid()%>"/>
								 		
										<!-- #section:elements.form -->
										<div class="form-group">
											<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="form-id">原密码</label>
											<div class="col-xs-12 col-sm-9">
												<div class="clearfix">
													<input type="password" name="oldpassword"  id="oldpassword" placeholder="" class="col-xs-12 col-sm-6" value="">
												</div>
											</div>
										</div>
									
									
									    <div class="space-2"></div>
									
										<div class="form-group">
											<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="form-name">新密码</label>
	
											<div class="col-xs-12 col-sm-9">
												<div class="clearfix">
													<input type="password" name="newpassword" id="newpassword" placeholder="" class="col-xs-12 col-sm-6" value="">
												</div>
											</div>
										</div>
										
									    <div class="space-2"></div>
									
										<div class="form-group">
											<label class="control-label col-xs-12 col-sm-3 no-padding-right" for="form-name">重复新密码</label>
	
											<div class="col-xs-12 col-sm-9">
												<div class="clearfix">
													<input type="password" name="newpassword2" id="newpassword2" placeholder="" class="col-xs-12 col-sm-6" value="">
												</div>
											</div>
										</div>
										<div class="hr hr-dotted"></div>
										<hr>
										
										<div class="clearfix form-actions">
											<div class="col-md-offset-3 col-md-9">
												<button class="btn  btn-inverse btn-lg no-border" type="button" onclick="gopage('${ctx}/system/desktop.jsp')"> 
													<i class="ace-icon glyphicon glyphicon-home bigger-160"></i>
													返回控制台
												</button>
												&nbsp; &nbsp; &nbsp;
												<button id="btn-save" name="btn-save" class="btn  btn-pink btn-lg no-border" type="button">
													<i class="ace-icon fa fa-floppy-o bigger-160"></i>
													保存
												</button>
											 </div>
										</div>
								 	</form>
								 		 	</div><!-- /.widget-main -->
										</div><!-- /.widget-body -->
									</div><!-- /.widget-box -->
								 </div><!-- /.col -->
							</div><!-- /.row -->
					</div><!-- /.page-content-area -->
			</div><!-- /.page-content -->
			
			<%@ include file="/common/common-js.jsp" %>
			<script type="text/javascript" src="${ctx}/js/md5.js"></script>
			<script>
			
				$(function(){
						
						//表单input改变校验
						$("input").on('change' , function(e, info){
							if(!$('#validation-form').valid()) return false;
						})
					
						//【提交】按钮Click事件		
						$("button[name='btn-save']").bind("click", function () { 
								dochangePwd();
		    			});
				});
		
				
			
			
				$('#validation-form').validate({
					errorElement: 'div',
					errorClass: 'help-block',
					focusInvalid: false,
					rules: {
						oldpassword: {
							required: true
						},
						newpassword: {
							required: true,
							regexPassword: true,  
							same:true
						},
						newpassword2: {
						    required: true, 
						    minlength: 6, 
						    equalTo: "#newpassword"  
						}
					},
			
					messages: {
						oldpassword: {
							required: "请输入老密码"
						},
						newpassword: {
							required: "请输入新密码",
							regexPassword:"必须包含字母和数字,长度至少6位的组合",
							same:"新密码不能与老密码一样"
						},
						newpassword2: {
						 	required: "请输入确认密码",  
                        	rangelength: "确认密码不能小于6个字符",  
                        	equalTo: "两次输入密码不一致"  
						}
					},
			
			
					highlight: function (e) {
						$(e).closest('.form-group').removeClass('has-info').addClass('has-error');
					},
			
					success: function (e) {
						$(e).closest('.form-group').removeClass('has-error');//.addClass('has-info');
						$(e).remove();
					},
			
					errorPlacement: function (error, element) {
						if(element.is('input[type=checkbox]') || element.is('input[type=radio]')) {
							var controls = element.closest('div[class*="col-"]');
							if(controls.find(':checkbox,:radio').length > 1) controls.append(error);
							else error.insertAfter(element.nextAll('.lbl:eq(0)').eq(0));
						}
						else if(element.is('.select2')) {
							error.insertAfter(element.siblings('[class*="select2-container"]:eq(0)'));
						}
						else if(element.is('.chosen-select')) {
							error.insertAfter(element.siblings('[class*="chosen-container"]:eq(0)'));
						}
						else error.insertAfter(element.parent());
					},
			
					submitHandler: function (form) {
					},
					invalidHandler: function (form) {
					}
				});
				
				
				jQuery.validator.addMethod("same", function(value, element) {  
               		 return this.optional(element) || same(value);  
            	}, "新密码不能与老密码重复");  
            	
            	jQuery.validator.addMethod("regexPassword", function(value, element) {  
    				return this.optional(element) || /^(?=.{6,})(((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$/.test(value);  
				}, "必须包含字母和数字,长度至少6位的组合");
            	
            	function same(pwd) {  
               	    var oldpassword = $("#oldpassword").val();  
	                if (oldpassword == pwd)  {
	                    return false;  
	                }else { 
	                    return true;  
	                }
            	}  
            	
            	
				function dochangePwd(){
					if(formCheck("validation-form")){
						console.log($("#oldpassword").val());
						console.log($("#newpassword").val());
						var params = {
							userId : $("#userid").val(),
							oldPwd : hex_md5($("#oldpassword").val()).toUpperCase(),
							newPwd : hex_md5($("#newpassword").val()).toUpperCase()
						};
						$("#btn-save").attr("disabled", "true");
						$.getJSON(ctx + "/system/user/changePwd.action", params, function(json){
							if("OK" == json.status){
								$(":password").val('');
								parent.bootbox.alert(json.message);
							}else{
								 parent.bootbox.alert(json.message);
							}
							$("#btn-save").removeAttr("disabled");
							return;
						});
					}
				}
				
				
				/****************************************************************************/
				/*
				 * 点击菜单，处理逻辑：
				 * (1)、打开相应界面 【add by yixiaoping】
				 * (2)、
				 * (3)、
				 */
				 /***************************************************************************/
				function gopage(url){
				  	parent.$("#main_frame").attr("src",url);//嵌套的主页面iframe id必须为main_frame
				}
			</script>
</body>
</html>
